import torch
import torch.nn as nn
import torch.nn.functional as F
from torchsummary import summary

class VGG19(nn.Module):
    def __init__(self):
        super().__init__()
        self.layer1 = VGGModule2(3, 64)
        self.bn1 = nn.BatchNorm2d(64)
        self.layer2 = VGGModule2(64, 128)
        self.bn2 = nn.BatchNorm2d(128)
        self.layer3 = VGGModule4(128, 256)
        self.bn3 = nn.BatchNorm2d(256)
        self.layer4 = VGGModule4(256, 512)
        self.layer5 = VGGModule4(512, 512)
        self.fn = VGGFN()
    def forward(self, x):
        x = self.layer1(x)
        x = self.bn1(x)
        x = self.layer2(x)
        x = self.bn2(x)
        x = self.layer3(x)
        x = self.bn3(x)
        x = self.layer4(x)
        x = self.layer5(x)
        x = self.fn(x)
        return x

class VGGModule2(nn.Module):
    def __init__(self, in_channles, out_channles):
        super().__init__()
        self.conv1 = nn.Conv2d(in_channles, out_channles, 3, 2, padding=8)
        self.conv2 = nn.Conv2d(out_channles, out_channles, 3, 2, padding=8)
        self.maxpool = nn.MaxPool2d(kernel_size=2)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = F.relu(self.conv2(x))
        x = self.maxpool(x)
        return x

class VGGModule4(nn.Module):
    def __init__(self, in_channles, out_channles):
        super().__init__()
        self.conv1 = nn.Conv2d(in_channles, out_channles, 3, 2, padding=8)
        self.conv2 = nn.Conv2d(out_channles, out_channles, 3, 2, padding=8)
        self.conv3 = nn.Conv2d(out_channles, out_channles, 3, 2, padding=8)
        self.conv4 = nn.Conv2d(out_channles, out_channles, 3, 2, padding=8)
        self.maxpool = nn.MaxPool2d(kernel_size=2)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = F.relu(self.conv2(x))
        x = F.relu(self.conv3(x))
        x = F.relu(self.conv4(x))
        x = self.maxpool(x)
        return x

class VGGFN(nn.Module):
    def __init__(self):
        super().__init__()
        self.maxpool = nn.MaxPool2d(2)
        self.fn1 = nn.Linear(4608, 4096)
        self.fn2 = nn.Linear(4096, 4096)
        self.fn3 = nn.Linear(4096, 10)

    def forward(self, x):
        x = self.maxpool(x)
        x = torch.flatten(x, start_dim=1)
        x = F.relu(self.fn1(x))
        x = F.relu(self.fn2(x))
        x = F.relu(self.fn3(x))
        return x


